﻿#region 命名空间

using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Install;

#endregion

namespace WinnerCMS.WebUI.Install
{
    public partial class Index : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Request.IsLocal)
            //{
            //    Response.Clear();
            //    Response.Write("为了安全，安装文件不能在服务器上面执行。");
            //    return;
            //}
            if (!IsPostBack)
            {
                this.Wizard1.ActiveStepIndex = 0;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var suss = true;
            this.lblErr.Text = "";
            try
            {
                WebConfig.ConnectionString = this.TtxtConnection.Text;
            }
            catch //(System.Exception Ex) 
            {
                //throw Ex; 
                suss = false;
                this.lblErr.Text += "请确保Web.config文件不是只读而且有可写权限。";
                MsgBox.Message("请确保Web.config文件不是只读而且有可写权限。", this.UpdatePanel1);
            }


            if (suss)
            {
                MsgBox.Message("设置成功！", this.UpdatePanel1);
            }
        }

        protected void btnTest2_Click(object sender, EventArgs e)
        {
            var suss = new CheckServerSetting().TestConnection(this.TtxtConnection.Text);
            if (suss)
                MsgBox.Message("连接数据库成功！", this.UpdatePanel1);
            else
                MsgBox.Message("连接数据库失败！", this.UpdatePanel1);
        }

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (e.NextStepIndex == 2)
            {
                var str = CheckServerSetting.CheckFile();
                if (str.Length == 0)
                    this.lblRunTime.Text = "检查配置成功!";
                else
                    this.lblRunTime.Text = str;
            }
            if (e.NextStepIndex == 3)
            {
                if (CheckServerSetting.IsVirtualDirectory())
                {
                    MsgBox.Message("尽量不要将改程序部署在虚拟目录里面，因为有部分功能会出错", this.UpdatePanel1);
                }
                this.TtxtConnection.Text = WebConfig.ConnectionString;
            }
            if (e.NextStepIndex == 4)
            {
                try
                {
                    this.txtSiteName.Text = WebConfig.SiteName;
                    this.txtWebSiteDomainName.Text = WebConfig.DomainName;
                    this.txtVirtualPath.Text = WebConfig.VirtualPath;

                    var config = CMSConfig.Instance();
                    this.txtWatherMakePath.Text = config.DrawingWater.DrawingWaterPath;
                    this.Ntxttransparent.Text = config.DrawingWater.Transparent.ToString();
                    this.Ntxtright.Text = config.DrawingWater.Right.ToString();
                    this.Ntxtbottom.Text = config.DrawingWater.Bottom.ToString();
                    this.txtUP.Text = config.UpLoadFolder;
                }
                catch (Exception ex)
                {
                    MsgBox.Message(ex.Message.Replace(@"\", @"\\"), this.UpdatePanel1);
                }
            }
            if (e.NextStepIndex == 5)
            {
                if (this.rblDBType.Items[0].Selected)
                {
                    this.cbkInit.Enabled = false;
                    this.lblIniErr.Text = "Access 数据库可跳过该步骤。";
                }
            }
            if (e.NextStepIndex == 6)
            {
                this.HyperLink1.NavigateUrl = WebConfig.BackGroundPath + "Login.aspx";
            }
        }

        protected void Wizard1_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
        {
        }

        protected void Wizard1_SideBarButtonClick(object sender, WizardNavigationEventArgs e)
        {
            //MsgBox.Alert("禁止跳过步骤。", UpdatePanel1);
            //e.Cancel = true;
            var i = e.NextStepIndex - e.CurrentStepIndex;
            if (Math.Abs(i) != 1)
            {
                MsgBox.Message("禁止跳过步骤。", this.UpdatePanel1);
                e.Cancel = true;
            }
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            Response.Redirect(WebConfig.BackGroundPath + "Login.aspx");
        }

        protected void btnSave2_Click(object sender, EventArgs e)
        {
            WebConfig.SiteName = this.txtSiteName.Text;
            WebConfig.DomainName = this.txtWebSiteDomainName.Text.Trim();
            WebConfig.VirtualPath = this.txtVirtualPath.Text.Trim();


            var config = CMSConfig.Instance();
            config.DrawingWater.DrawingWaterPath = this.txtWatherMakePath.Text;
            config.DrawingWater.Transparent = Convert.ToInt32(this.Ntxttransparent.Text);
            config.DrawingWater.Right = Convert.ToInt32(this.Ntxtright.Text);
            config.DrawingWater.Bottom = Convert.ToInt32(this.Ntxtbottom.Text);
            config.UpLoadFolder = this.txtUP.Text;
            config.Save(config);

            MsgBox.Message("保存成功!", this.UpdatePanel1);
        }

        protected void cbkInit_CheckedChanged(object sender, EventArgs e)
        {
            this.btnInit.Enabled = this.cbkInit.Checked;
        }

        protected void btnInit_Click(object sender, EventArgs e)
        {
            var i = new InitData();

            i.BuildObject();

            i.InsertMustData();

            MsgBox.Message("操纵成功!", this.UpdatePanel1);
        }
    }

    internal enum DataBaseSort
    {
        Sql = 0,
        MySql = 2,
        Ce = 4,
        Oracle = 8,
        Sqlite = 16
    }
}